<?php

class addheroModel extends Model{
     private function updateImg($img){
        $fileName = $img["name"];
        $upload_path = "./public/upload/";
        $newname = substr_replace($fileName, date("YmdHis") . rand(100, 999), 0, strrchr(".", $fileName));
        $url = $upload_path . $newname;
        if (move_uploaded_file($img["tmp_name"], $url)) {
            return $url;
        }
    }
    public function addHero(){
        $img = $this->updateImg($_FILES['img']);
        $big_img = $this->updateImg($_FILES['big_img']);
        $pimg = $this->updateImg($_FILES['pimg']);
        $qimg = $this->updateImg($_FILES['qimg']);
        $wimg = $this->updateImg($_FILES['wimg']);
        $eimg = $this->updateImg($_FILES['eimg']);
        $rimg = $this->updateImg($_FILES['rimg']);
        $name = isset($_POST['name'])?$_POST['name']:"";
        $big_name = isset($_POST['big_name'])?$_POST['big_name']:"";
        $type = isset($_POST['type'])?$_POST['type']:"";
        $live = isset($_POST['live'])?$_POST['live']:"";
        $physics = isset($_POST['physics'])?$_POST['physics']:"";
        $magic = isset($_POST['magic'])?$_POST['magic']:"";
        $difficulty = isset($_POST['difficulty'])?$_POST['difficulty']:"";

        $sql = "insert into hero (name,big_name,img,big_img,type,live,physics,magic,difficulty) values ('$name','$big_name','$img','$big_img',$type,$live,$physics,$magic,$difficulty)";
        $herolink = $this->link->query($sql);
        $id = $this->link->insterId;
        if(!$herolink){
            return false;
        }

        $pname = $_POST['pname'];
        $pd = $_POST['pd'];
        $sql = "insert into h_passive (hid,name,passive_img,`describe`) values ($id,'$pname','$pimg','$pd')";
        $plink = $this->link->query($sql);
        if(!$plink){
            return false;
        }

        $qname = $_POST['qname'];
        $qd = $_POST['qd'];
        $sql = "insert into h_q (hid,name,q_img,`describe`) values ($id,'$qname','$qimg','$qd')";
        $qlink = $this->link->query($sql);
        if(!$qlink){
            return false;
        }

        $wname = $_POST['wname'];
        $wd = $_POST['wd'];
        $sql = "insert into h_w (hid,name,w_img,`describe`) values ($id,'$wname','$wimg','$wd')";
        $wlink = $this->link->query($sql);
        if(!$wlink){
            return false;
        }

         $ename = $_POST['ename'];
        $ed = $_POST['ed'];
        $sql = "insert into h_e (hid,name,e_img,`describe`) values ($id,'$ename','$eimg','$ed')";
        $elink = $this->link->query($sql);
        if(!$elink){
            return false;
        }

        $rname = $_POST['rname'];
        $rd = $_POST['rd'];
        $sql = "insert into h_r (hid,name,r_img,`describe`) values ($id,'$rname','$rimg','$rd')";
        $rlink = $this->link->query($sql);
        if(!$rlink){
            return false;
        }
        return true;
    }
}